/**
 * temp stationMonitor
 */
import posts from '@/plugins/request'
import storage from "@/plugins/localStorage"
// initial state
const state = {
  stationInfoList: [],
  statisticsData: {},
  monitorListData: [], //监控列表
  workTermData: [], //工作条件
  runtimeRecord: { //仪器有效运维记录
    data: [],
    total: 0
  },
  reportForms: {
    data: [],
    total: 0
  },

  warningPanel: [],

  mobileStation: [],

  onlineCarSatatus: false, //实时车辆的状态 false = 休息中 || true = 工作中


  faultDevices: [], //故障设备
  excessStations: [], //超标站点
  stationsInfo: [],//站点数量
  monitorData: {
    data: [],
    oneHourData: [],
    realdata: [],
    pollutions: [],
    factorField: [],
    factorStatus: {},
    outletDataList: []
  },
}
// getters
const getters = {
  getStationInfoList: state => state.stationInfoList,
  getStatisticsData: state => state.statisticsData,
  getMonitorListData: state => state.monitorListData,
  getWorkTermData: state => state.workTermData,
  getRuntimeRecord: state => state.runtimeRecord,
  getReportForms: state => state.reportForms,
  getRealTimeWarningPanel: state => state.warningPanel,
  getMobileStation: state => state.mobileStation,
  getOnlineCarSatatus: state => state.onlineCarSatatus,
  getFaultDevices: state => state.faultDevices,
  getExcessStations: state => state.excessStations,
  getStationsInfo: state => state.stationsInfo,
  getMonitorData: state => state.monitorData
}
// actions
const actions = {
  async fetchRuntimeRecord({ commit }, params) {
    let data = await posts.fetchRuntimeRecord(params)
    commit('changeRuntimeRecord', data)
  },
  async getStationMap({ commit }) {
    let data = await posts.fetchStationMonMap()
    commit('changeStationInfoList', data.data.concat(data.forwardingStation || []))
    commit('changeMobileStationData', data.mobileStation)
    // commit('changeStatisticsData', data.mapInfo)
  },
  async getStationData({ commit }) {
    let res = await posts.fetchStationNewMonMap()
    let mapInfo = {
      exceedRate: res.data.exceedRate,//达标率
      exceedStandard: res.data.exceedStandard,//超标站点数量
      stationCount: res.data.stationCount,//站点数量
      malfunctionRate: res.data.malfunctionRate,//故障率
      malfunction: res.data.malfunction,//设备故障数量
    }
    commit('changeMonitorData', res.data)
    commit('changeStatisticsData', mapInfo)
  },
  async fetchReportFormsList({ commit }, param) {
    let data = await posts.fetchReportFormsList(param)
    commit('changeReportForms', data)
  },
  async fetchRealTimeWarning({ commit }) {
    let data = await posts.fetchRealTimeWarning()
    commit('changeRealTimeWarning', data)
  },
  async fetchStationsInfo({ commit }) {
    let data = await posts.fetchStationsInfo()
    commit('changeStationsInfo', data)
  },
  async fetchFaultDevices({ commit }) {
    let data = await posts.fetchFaultDevices()
    commit('changeFaultDevices', data)
  },
  async fetchExcessStations({ commit }) {
    let data = await posts.fetchExcessStations()
    commit('changeExcessStations', data)
  },
}

// mutations
const mutations = {
  changeStationInfoList(state, data) {
    state.stationInfoList = data
  },
  changeStationsInfo(state, data) {
    state.stationsInfo = data
  },
  changeStatisticsData(state, data) {
    state.statisticsData = data
  },
  changeMonitorListData(state, data) {
    state.monitorListData = data
  },
  changeMonitorData(state, data) {
    state.monitorData.data = data ? data.listdata : []
    state.monitorData.realdata = data ? data.realdata : []
    state.monitorData.oneHourData = data ? data.oneHourData : []
    state.monitorData.pollutions = data ? data.pollutions : []
    state.monitorData.outletDataList = data ? data.outletDataList : []
    state.monitorData.factorStatus = data ? data.factorStatus : 0
    state.monitorData.factorField = []
    let standard = {}
    data.factorMessage.forEach((item) => {
      if (Object.keys(data.factorField).includes(item.name)) {
        if (data.factorField[item.name] === 'ph') {
          standard[data.factorField[item.name]] = {}
          standard[data.factorField[item.name]]["max"] = Number(item.standardValue.split("~")[1])
          standard[data.factorField[item.name]]["min"] = Number(item.standardValue.split("~")[0])
        } else {
          standard[data.factorField[item.name]] = item.standardValue
        }
        state.monitorData.factorField.push({
          prop: data.factorField[item.name],
          label: item.name + (item.name === "pH" ? "" : " mg/L"),
          devStatus: item.name,
          standardValue: item.standardValue,
          factorLimit:
            item.name === "pH"
              ? "±" + item.factorLimit + "L"
              : item.factorLimit + "L",
        });
      }
    })
    storage.set("standard", standard)
  },
  changeWorkTermData(state, data) {
    state.workTermData = data
  },
  changeRuntimeRecord(state, data) {
    state.runtimeRecord.data = data.rows
    state.runtimeRecord.total = data.total
  },
  changeReportForms(state, data) {
    state.reportForms.data = data.rows
    state.reportForms.total = data.total
  },
  changeRealTimeWarning(state, data) {
    for (let i = 0; i < data.length; i += 3) {
      state.warningPanel.push(data.slice(i, i + 3))
    }
  },
  changeMobileStationData(state, data) {
    state.mobileStation = data
  },
  changeOnlineCarSatatus(state, flag) {
    state.onlineCarSatatus = flag
  },
  changeFaultDevices(state, data) {
    state.faultDevices = data
  },
  changeExcessStations(state, data) {
    state.excessStations = data
  }
}
export default {
  state,
  getters,
  actions,
  mutations
}